package com.example.jh352160.surfingtv.mypage.childPage;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.mypage.customView.MyPageSettingItem;
import com.example.jh352160.surfingtv.mypage.customView.ProfileImageView;
import com.example.jh352160.surfingtv.network.bean.EditDataBean;
import com.example.jh352160.surfingtv.network.item.RequestItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.MyDataService;

import java.io.File;
import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/27
 */

public class EditPage extends AppCompatActivity {

    MyPageSettingItem item1, item2, item3;
    ProfileImageView profileImageView;
    TextView logout;
    Button sexButton;
    boolean isMale;
    Uri imageUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mypage_edit);
        item1 = (MyPageSettingItem) findViewById(R.id.editView1);
        item2 = (MyPageSettingItem) findViewById(R.id.editView2);
        item3 = (MyPageSettingItem) findViewById(R.id.editView3);
        logout = (TextView) findViewById(R.id.logout);
        profileImageView = (ProfileImageView) findViewById(R.id.profileImageView);
        sexButton = (Button) item2.findViewById(R.id.button);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String age = intent.getStringExtra("age");
        isMale = intent.getBooleanExtra("isMale", true);
        String photoUrl = intent.getStringExtra("photoUrl");

        Glide.with(this).load("http://app4tv.sudaotech.com/platform/image/" + photoUrl).placeholder(R.drawable.mypage_portrait).into(profileImageView);
        item1.setView("用户名", name, InputType.TYPE_CLASS_TEXT);
        item2.setView("性别", "", InputType.TYPE_CLASS_TEXT, true, isMale);
        item3.setView("年龄", age, InputType.TYPE_CLASS_NUMBER);

        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText nicknameView = (EditText) item1.findViewById(R.id.edittext);
                EditText ageView = (EditText) item3.findViewById(R.id.edittext);
                String nickname = nicknameView.getText().toString();
                int gander = isMale ? 1 : 0;
                int age = Integer.parseInt(ageView.getText().toString());
                EditDataBean editDataBean = new EditDataBean(nickname, gander, age);
                Retrofit retrofit = CommonRetrofit.getRetrofit();
                MyDataService myDataService = retrofit.create(MyDataService.class);
                Call<RequestItem> call = myDataService.editData(editDataBean);
                call.enqueue(new Callback<RequestItem>() {
                    @Override
                    public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                        if (response.body().getCode().equals("200")) {
                            Toast.makeText(EditPage.this, "保存成功", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<RequestItem> call, Throwable t) {

                    }
                });
            }
        });

        profileImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                File output = new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
                try {
                    if (output.exists()) {
                        output.delete();
                    }
                    output.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                imageUri = Uri.fromFile(output);
                Intent intent = new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                intent.putExtra("crop", true);
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            try {
//                Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                Uri uri = data.getData();
//                profileImageView.setImageBitmap(bitmap);
                Glide.with(EditPage.this).load(uri).into(profileImageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
